<?php
include_once 'event.php';

/*-------------------------------------------------------------------------------*/
/*--- Fonction qui retourne un tableau d'évènements à partir d'un fichier XML ---*/
/*-------------------------------------------------------------------------------*/
function parseXML($xml_file){
	$timetable = array();

	$dom = new DOMDocument();
	if (!$dom->load($xml_file))
		echo "Erreur durant le chargement du fichier XML.";

	//On récupère les évenements
	$eventList = $dom->getElementsByTagName("event");

	foreach($eventList as $event){
		$evt = new Event();
			
		$date = $event->getAttribute("date");
		$evt->setDate($date);

		// Les infos de l'évènement
		$eventDatas = $event->childNodes;

		foreach($eventDatas as $data){
			switch($data->nodeName){
				case "day":
					$evt->setDay($data->textContent);
					break;
				case "starttime":
					$evt->setStartTime($data->textContent);
					break;
				case "endtime":
					$evt->setEndTime($data->textContent);
					break;
				case "category":
					$evt->setCategory($data->textContent);
					break;
				case "resources":
					$resourcesInfos = $data->childNodes;
					foreach($resourcesInfos as $info){
						if($info->nodeName == "module"){
							$module = $info->lastChild->textContent;
							if($module != NULL)
							$evt->setModule($module);
						}
						if($info->nodeName == "room"){
							$res = "";
							$rooms = $info->childNodes;
							foreach($rooms as $room)
							if($room->nodeName == "item")
							$res = $res." ".$room->textContent;
							$evt->setRooms($res);
						}
					}
					break;
				case "notes":
					$evt->setNotes($data->textContent);
					break;
				default:
					break;
			}
		}

		//Rajout de l'évenement dans l'edt
		$timetable[] = $evt;
	}

	return $timetable;
}

/*---------------------------------------------------------*/
/*--------Retourne les différents modules de l'edt --------*/
/*---------------------------------------------------------*/
function getModulesFromTimetable($timetable){
	$modules = array();
	foreach($timetable as $event){
		if(! in_array($event->getModule(), $modules))
		$modules[] = $event->getModule();
	}
	return $modules;
}

/*----------------------------------------------------------*/
/*----- Retourne les différentes catégories de l'edt -------*/
/*----------------------------------------------------------*/
function getCategoriesFromTimetable($timetable){
	$categories= array();
	foreach($timetable as $event){
		if(! in_array($event->getCategory(), $categories))
		$categories[] = $event->getCategory();
	}
	return $categories;
}

/*----------------------------------------------------------------------*/
/*----- Retourne l'ensemble des formations du fichier xml Celcat -------*/
/*----------------------------------------------------------------------*/
function parseCelcatFinder($xml_file){
	$findertable = array();	
	$dom = new DOMDocument();
	if (!$dom->load($xml_file))
		echo "Erreur durant le chargement du fichier XML.";

	$resourceNodes = $dom->getElementsByTagName("resource");
	
	foreach($resourceNodes as $resource){
		$resource_infos = array();		
		$link = $resource->getAttribute("link");
		$link = str_replace(".html",".xml",$link);
		$resource_infos['link'] = $link;
		$resource_childs = $resource->childNodes;
		foreach($resource_childs as $r_child){
			if($r_child->nodeName == "name")
			$resource_infos['name'] = $r_child->textContent;
		}

		$findertable[] = $resource_infos;
	}
	return $findertable;
}


?>
